package com.example.bgImageSear.user.controller.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.regex.Pattern;

public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();
        boolean canAccess = !Pattern.matches("/comment/commentInsert|/bgData/start|/user/setting", uri);
        if (canAccess) {
            return true;
        }

        Object uid = session.getAttribute("uid");
        boolean isLogin = uid != null;
        if (isLogin) {
            request.setAttribute("uid", uid);
            return true;
        }
        response.setStatus(403);
        return false;
    }
}